package com.wjb.xietong.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import com.wjb.xietong.control.WJBControl;
import com.wjb.xietong.datamanager.LoginUserDataManager;
import com.wjb.xietong.server.IRequestResultListener;
import com.wjb.xietong.server.topic.MessageNotifyParam;
import com.wjb.xietong.server.topic.MessageNotifyResponse;
import com.wjb.xietong.util.EnumDefine.Login_User_Info;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class TopicTrigger_MGR implements TopicObserver {
	private volatile static TopicTrigger_MGR instance;

	private ArrayList<TopicTriggerListener> observers = new ArrayList<TopicTriggerListener>();

	private ScheduledThreadPoolExecutor executor = null;

	private TopicTrigger_MGR() {
		super();
		init();
	}

	public static TopicTrigger_MGR Instance() {
		if (instance == null) {
			synchronized (TopicTrigger_MGR.class) {
				if (instance == null) {
					instance = new TopicTrigger_MGR();
				}
			}
		}
		return instance;
	}

	private void init() {
		requestMessageNotifyTask();
	}

	/**
	 * 请求轮询消息通知信息
	 */
	public void requestMessageNotifyTask() {
		executor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {

			@Override
			public Thread newThread(Runnable task) {
				return new Thread(task, "MessageNotify");
			}
		});
		// 每隔10S请求查询一次,首次进入延时10S后执行
		executor.scheduleWithFixedDelay(new Runnable() {

			@Override
			public void run() {
				requestMessageNotify();
			}
		}, 10, 10, TimeUnit.SECONDS);
	}

	/**
	 * 查询通知信息
	 */
	private void requestMessageNotify() {
		MessageNotifyParam param = new MessageNotifyParam();
		param.setUserId((Long) LoginUserDataManager
				.getLoginUserInfo(Login_User_Info.ID));
		param.setCompanyId((Long) LoginUserDataManager
				.getLoginUserInfo(Login_User_Info.COM_ID));
		WJBControl.messageNotify(Calendar.getInstance().getTimeInMillis(),
				param, new IRequestResultListener() {

					@Override
					public void requestSuccess(long requestUniqueID) {
						int allCount = MessageNotifyResponse.instance()
								.getAll_retopic_count();

						if (0 != allCount) {
							TopicTrigger topicTrigger = new TopicTrigger();
							topicTrigger
									.setTriggerID(TopicTriggerID.New_Message_Notify);
							topicTrigger.setlParam1(allCount);
							notifyTopicbserver(topicTrigger);
						}
					}

					@Override
					public void requestFaield(long requestUniqueID,
							String errCode, String errMsg) {
						// TODO
					}
				});
	}

	@Override
	public void registerTopicObserver(TopicTriggerListener observer) {
		if (!observers.contains(observer)) {
			observers.add(observer);
		}
	}

	@Override
	public void removeTopicObserver(TopicTriggerListener observer) {
		int index = observers.indexOf(observer);
		if (index >= 0) {
			observers.remove(index);
		}
	}

	@Override
	public void notifyTopicbserver(final TopicTrigger trigger) {
		new Handler(Looper.getMainLooper()) {

			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					for (int i = 0; i < observers.size(); i++) {
						TopicTriggerListener observer = (TopicTriggerListener) observers
								.get(i);
						observer.onTopicTrigger(trigger);
					}
				}
				super.handleMessage(msg);
			}

		}.sendEmptyMessage(1);
	}

}
